﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebChatServer.Models;

namespace WebChatServer.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class RegisterController : ApiController
    {
        
        IMemberRepository _repository = new MemberRepository();
        ChatRepository chatRepository = new ChatRepository();

        public ChatMember Post(Register member)
        {
            if (_repository.UserNameIsExist(member))
            {
                chatRepository.Status(member.UserName, 1);
                member = _repository.AddMember(member);               
                var response = Request.CreateResponse<Register>(HttpStatusCode.Created, member);

                string uri = Url.Link("DefaultApi", new {id = member});
                response.Headers.Location = new Uri(uri);

                if(member != null)
                    return _repository.Get(member.UserName);
            }
            return null;
        }

    }
}
